//
//  RePasscodeViewController.m
//  KurtulusLaw
//
//  Created by phuc phuc on 12/2/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "RePasscodeViewController.h"
#import "CCommon.h"

@implementation RePasscodeViewController
@synthesize lb1;
@synthesize text4;
@synthesize text3;
@synthesize text2;
@synthesize text1;
@synthesize number1,number2,number3,number4;
-(void)processCode
{
    if ([self.text1.text isEqualToString:self.number1]&&[self.text2.text isEqualToString:self.number2]&&[self.text3.text isEqualToString:self.number3]&&[self.text4.text isEqualToString:self.number4]) {
        [CCommon resetPasscode];
        [CCommon createPasscode:self.text1.text :self.text2.text :self.text3.text :self.text4.text];
        [self cancel:self];
    }
    else
    {
        [self.navigationController popViewControllerAnimated:YES];
    }
}
-(IBAction)textChanged : (id)sender
{
    UITextField *text = (UITextField*)sender;
    if (text.tag == 1) {
        [self.text2 becomeFirstResponder];
    }
    else if (text.tag == 2)
    {
        [self.text3 becomeFirstResponder];
    }
    else if (text.tag == 3)
    {
        [self.text4 becomeFirstResponder];
    }
    else if (text.tag == 4)
    {
        [self processCode];
    }
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = NSLocalizedString(@"Passcode", nil);
	self.lb1.text = NSLocalizedString(@"Re enter your Passcode", nil);
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bgback.png"]];
    self.navigationItem.hidesBackButton = YES;
    self.navigationController.navigationItem.backBarButtonItem = nil;
    self.navigationController.navigationItem.leftBarButtonItem = nil;
    
    UIButton *btnCancel = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 67, 24)];
    [btnCancel setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
	
    [btnCancel setTitle: NSLocalizedString(@"Cancel", nil) forState:UIControlStateNormal];
    btnCancel.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"logout-67x24.png"]];
    btnCancel.titleLabel.font = [UIFont boldSystemFontOfSize:16.0f];
    btnCancel.titleLabel.textColor = [UIColor grayColor];
    [btnCancel addTarget:self action:@selector(cancelPasscode:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btnCancel];
     [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"nav-bg-black"] forBarMetrics:UIBarMetricsDefault] ;
}
-(void)cancelPasscode : (id)sender
{
    [self dismissModalViewControllerAnimated:YES];
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.text1.text = @"";
    self.text2.text = @"";
    self.text3.text = @"";
    self.text4.text = @"";
    [self.text1 becomeFirstResponder];
    
}
- (void)viewDidUnload
{
    [self setText1:nil];
    [self setText2:nil];
    [self setText3:nil];
    [self setText4:nil];
    [self setText2:nil];
    [self setText2:nil];
    [self setText3:nil];
    [self setText4:nil];
    [self setText2:nil];
    [self setText1:nil];
    [self setText2:nil];
    [self setText3:nil];
    [self setText4:nil];
    [self setLb1:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (IBAction)cancel:(id)sender {
    [self dismissModalViewControllerAnimated:YES];
}
@end
